/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：PwdQuesDaoImpl.java
 * History:
 *       May 11, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import net.sf.json.JSONObject;

import com.xteam.ecsystem.dao.IPwdQuesDao;
import com.xteam.ecsystem.entities.BaseMemberBase;
import com.xteam.ecsystem.entities.BasePasswordQuestion;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 密保问题数据访问实现类
 * 
 * @author liuzhi
 *
 */
@Repository
@SuppressWarnings("unchecked")
public class PwdQuesDaoImpl extends DDLPrefix implements IPwdQuesDao {
	
	@Autowired
	private BaseDaoImpl baseDao;

	public void delObject(String ids) {
		Class clazz = null;
		
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.BasePasswordQuestion");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		List list = new ArrayList();
		String oId[] = ids.split(",");
		for (String id : oId) {
			Object obj = this.baseDao.get(clazz, id);
			if (obj != null) {
				list.add(obj);
			}
		}
		this.baseDao.deleteAll(list);
	}

	public String getBpqList(int pageIndex, int pageSize) {
		JSONObject jo = new JSONObject();
		
		try {
			String hql = SQL_FROM + BasePasswordQuestion.REF + SQL_WHERE +" 1=1 ";
			String countHQL = SQL_SELECT + " count(*) " + SQL_FROM 
					+ BasePasswordQuestion.REF + SQL_WHERE +" 1=1 ";
			

			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao
					.queryList(countHQL, hql, null, pageIndex * pageSize, pageSize);
			
			jo = JSONObject.fromObject(p);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	public void saveOrUpdateObject(Object obj, String id) {
		try {
			BasePasswordQuestion bpq = (BasePasswordQuestion)obj;
			if (id != null && !id.equals("") && id.length() > 0){
				this.baseDao.saveOrUpdate(bpq);
			}else {
				this.baseDao.save(bpq);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getBpqById(String id) {
		JSONObject jo = new JSONObject();
		
		try {
			List list = new ArrayList();
			
			String hql = SQL_FROM + BasePasswordQuestion.REF + SQL_WHERE 
					+" 1=1 " + SQL_AND + " bpqId=?";
			list = this.baseDao.find(hql, new Object[]{id});
			
			jo = JSONObject.fromObject((BasePasswordQuestion)list.get(0));

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	public void updateBpqStateById(String id) {
		Class clazz = null;
		
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.BasePasswordQuestion");
			
			BasePasswordQuestion bpq = (BasePasswordQuestion)this.baseDao.get(clazz, id);
			if(bpq.getBpqState() == 1){
				bpq.setBpqState(0);
			}else{
				bpq.setBpqState(1);
			}
			
			this.baseDao.saveOrUpdate(bpq);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public boolean getBmbBybpq(String bpqIds){
		boolean isUsed = false;
		List list = new ArrayList();
		String hql = "";
		
		try {
			String oId[] = bpqIds.split(",");
			for (String id : oId) {
				hql = SQL_FROM + BaseMemberBase.REF + SQL_WHERE + "bmbQuestion = ?";
				list = this.baseDao.find(hql, new Object[]{id});
				
				if (list != null && !list.isEmpty()) {
					isUsed = true;
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return isUsed;
	}
}
